/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.Resource;
import sunsite.service.FileService;
import sunsite.tools.FileHelper;
import sunsite.po.DownloadHistory;

/**
 *
 * @author cyp
 */
public class FileDownload extends ActionSupport {

    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Register.class);

    private String type;
    private List<sunsite.po.Resource> ResourceList;
    private String resId;    
    private String fileName;
    private String inputPath;//文件路径
    private ResourceBundleMessageSource messageSource;
    private FileService fileService;

    @Override
    public String execute() throws Exception {
        if (type != null && type.equals("getForm")) {//初始化
            ResourceList=fileService.getAllResource();
            return INPUT;
        }
        if (type != null && type.equals("download")) {//下载
            if(resId!=null && !resId.equals("0")){
                Resource resource = fileService.getResource(getResId());
                resource.setCount(resource.getCount()+1);

                
                String suffix=FileHelper.getExtention(inputPath);//后缀
                fileName=resource.getResName()+suffix;
                inputPath=resource.getAddress();
                String hisid=new Date().getTime()+"";
                String ip = ServletActionContext.getRequest().getRemoteAddr();
                DownloadHistory history= new DownloadHistory();
                
                history.setHisId(hisid);
                history.setResource(resource);
                history.setDownIp(ip);
                //history.setUsr(usr);
                fileService.modifyResource(resource);
                fileService.addDownloadHistory(history);
            }
            return SUCCESS;
        }

        return SUCCESS;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the ResourceList
     */
    public List<sunsite.po.Resource> getResourceList() {
        return ResourceList;
    }

    /**
     * @param ResourceList the ResourceList to set
     */
    public void setResourceList(List<sunsite.po.Resource> ResourceList) {
        this.ResourceList = ResourceList;
    }

    /**
     * @return the resId
     */
    public String getResId() {
        return resId;
    }

    /**
     * @param resId the resId to set
     */
    public void setResId(String resId) {
        this.resId = resId;
    }

    /**
     * @return the messageSource
     */
    public ResourceBundleMessageSource getMessageSource() {
        return messageSource;
    }

    /**
     * @param messageSource the messageSource to set
     */
    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    /**
     * @return the fileService
     */
    public FileService getFileService() {
        return fileService;
    }

    /**
     * @param fileService the fileService to set
     */
    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }
    public InputStream getInputStream() throws Exception {
        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }
    public void setInputPath(String inputPath) {
    this.inputPath = inputPath;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getDownloadFileName() throws UnsupportedEncodingException {
        String downFileName = fileName;
        try {
            downFileName = new String(downFileName.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return downFileName;
    }
}
